// 简化的数据库初始化脚本
import { dbService, dataService, COLLECTIONS } from '../services/simpleDbService.js'

// 初始用户数据
const initialUsers = [
  {
    id: 'user1',
    username: 'admin',
    password: 'admin123', // 实际项目中应使用加密存储
    email: 'admin@example.com',
    avatar: '/src/assets/logo.png',
    bio: '系统管理员',
    description: '全栈开发者 | 技术爱好者 | 摄影发烧友',
    website: 'https://example.com/admin',
    posts: 42,
    followers: 1253,
    following: 28,
    likes: 15420,
    isVip: true,
    role: 'admin',
    createdAt: new Date().toISOString()
  },
  {
    id: 'user2',
    username: 'user',
    password: 'user123', // 实际项目中应使用加密存储
    email: 'user@example.com',
    avatar: '/src/assets/logo.png',
    bio: '普通用户',
    description: '热爱生活，喜欢分享日常点滴',
    website: 'https://example.com/user',
    posts: 12,
    followers: 42,
    following: 86,
    likes: 785,
    isVip: false,
    role: 'user',
    createdAt: new Date().toISOString()
  }
]

// 初始分类数据
const initialCategories = [
  { id: 'anime', name: '动画', icon: 'fa-tv' },
  { id: 'music', name: '音乐', icon: 'fa-music' },
  { id: 'game', name: '游戏', icon: 'fa-gamepad' },
  { id: 'entertainment', name: '娱乐', icon: 'fa-star' },
  { id: 'knowledge', name: '知识', icon: 'fa-book' },
  { id: 'tech', name: '科技', icon: 'fa-microchip' },
  { id: 'life', name: '生活', icon: 'fa-home' },
  { id: 'variety', name: '综艺', icon: 'fa-film' },
  { id: 'food', name: '美食', icon: 'fa-utensils' },
  { id: 'sports', name: '运动', icon: 'fa-running' }
]

// 初始视频数据
const initialVideos = [
  {
    id: 'video1',
    title: 'Vue3框架入门教程',
    description: '全面讲解Vue3的核心概念和使用方法，从基础到进阶的完整教程。',
    thumbnail: '/src/assets/科技数码展宣传图.jpg',
    videoUrl: 'https://example.com/videos/vue3-tutorial.mp4',
    category: 'knowledge',
    tags: ['Vue3', '教程', '前端'],
    authorId: 'user1',
    views: 1234,
    likes: 23000,
    comments: 1283,
    bulletCount: 32,
    qualityBadge: '高清',
    duration: '45:20',
    uploadTime: '2024-01-15T10:30:00Z',
    isHot: true,
    isRecommended: true,
    createdAt: new Date().toISOString()
  },
  {
    id: 'video2',
    title: '现代前端开发工作流',
    description: '提升开发效率的完整工作流程搭建，包含版本控制、自动化构建等。',
    thumbnail: '/src/assets/二次元美食节海报_3.jpg',
    videoUrl: 'https://example.com/videos/workflow.mp4',
    category: 'tech',
    tags: ['工作流', '开发', '效率'],
    authorId: 'user1',
    views: 5678,
    likes: 18000,
    comments: 956,
    bulletCount: 120,
    qualityBadge: '超清',
    duration: '38:15',
    uploadTime: '2024-02-10T14:20:00Z',
    isHot: true,
    isRecommended: true,
    createdAt: new Date().toISOString()
  },
  {
    id: 'video3',
    title: '响应式设计最佳实践',
    description: '响应式网页设计的实用技巧和案例分析，让你的网站在各种设备上都有出色表现。',
    thumbnail: '/src/assets/鬼灭之刃.jpeg',
    videoUrl: 'https://example.com/videos/responsive-design.mp4',
    category: 'knowledge',
    tags: ['响应式', '设计', 'UI'],
    authorId: 'user2',
    views: 8901,
    likes: 31000,
    comments: 1542,
    bulletCount: 256,
    qualityBadge: '高清',
    duration: '52:40',
    uploadTime: '2024-03-05T09:15:00Z',
    isHot: true,
    isRecommended: true,
    createdAt: new Date().toISOString()
  },
  {
    id: 'video4',
    title: 'CSS动画效果教程',
    description: '从基础到高级的CSS动画技巧教学，让你的页面更加生动有趣。',
    thumbnail: '/src/assets/黑神话.jpeg',
    videoUrl: 'https://example.com/videos/css-animation.mp4',
    category: 'knowledge',
    tags: ['CSS', '动画', '前端'],
    authorId: 'user2',
    views: 3456,
    likes: 25000,
    comments: 1128,
    bulletCount: 89,
    qualityBadge: '超清',
    duration: '41:30',
    uploadTime: '2024-03-20T16:45:00Z',
    isHot: false,
    isRecommended: true,
    createdAt: new Date().toISOString()
  }
]

// 初始标签数据
const initialTags = [
  { name: '新番推荐', color: '#FF6B6B', icon: 'fa-tv', route: '/anime?tab=new' },
  { name: '音乐现场', color: '#4ECDC4', icon: 'fa-music', route: '/music' },
  { name: '游戏攻略', color: '#FFE66D', icon: 'fa-gamepad', route: '/game' },
  { name: '科技前沿', color: '#1A535C', icon: 'fa-microchip', route: '/tech' },
  { name: '美食探店', color: '#F7B801', icon: 'fa-utensils', route: '/food' },
  { name: '运动健身', color: '#6B48FF', icon: 'fa-running', route: '/sports' },
  { name: '宠物日常', color: '#FF9A8B', icon: 'fa-paw', route: '/animal' },
  { name: '电影解说', color: '#2E86AB', icon: 'fa-film', route: '/movie' },
  { name: 'Vue3', color: '#42B883', icon: 'fa-code', route: '/knowledge' },
  { name: '教程', color: '#61DAFB', icon: 'fa-book', route: '/knowledge' },
  { name: '前端', color: '#F06529', icon: 'fa-code', route: '/tech' },
  { name: '工作流', color: '#4B8BF5', icon: 'fa-sitemap', route: '/tech' },
  { name: '开发', color: '#6E40C9', icon: 'fa-laptop-code', route: '/tech' },
  { name: '效率', color: '#10B981', icon: 'fa-tachometer-alt', route: '/life' },
  { name: '响应式', color: '#0EA5E9', icon: 'fa-mobile-alt', route: '/knowledge' },
  { name: '设计', color: '#F472B6', icon: 'fa-paint-brush', route: '/knowledge' },
  { name: 'UI', color: '#EC4899', icon: 'fa-desktop', route: '/knowledge' },
  { name: 'CSS', color: '#1572B6', icon: 'fa-css3-alt', route: '/knowledge' },
  { name: '动画', color: '#F43F5E', icon: 'fa-film', route: '/knowledge' },
  { name: '生活技巧', color: '#3B82F6', icon: 'fa-lightbulb', route: '/life' }
]

// 初始话题数据
const initialTopics = [
  {
    id: 'topic1',
    title: 'Vue3的新特性到底有多强？',
    description: '讨论Vue3框架带来的性能提升和开发体验改进，以及实际项目中的应用案例。',
    hot: '35.2万',
    participants: 12530,
    comments: 8952,
    creatorId: 'user1',
    createdAt: new Date().toISOString()
  },
  {
    id: 'topic2',
    title: '2024年前端开发趋势预测',
    description: '探讨2024年前端开发领域可能出现的新技术、新框架和新方向。',
    hot: '28.7万',
    participants: 9856,
    comments: 7243,
    creatorId: 'user1',
    createdAt: new Date().toISOString()
  },
  {
    id: 'topic3',
    title: '如何提升代码质量和可维护性？',
    description: '分享提升代码质量的实践经验、代码审查技巧和团队协作方法。',
    hot: '22.3万',
    participants: 7621,
    comments: 5832,
    creatorId: 'user2',
    createdAt: new Date().toISOString()
  }
]

// 初始活动数据
const initialActivities = [
  {
    id: 'activity1',
    title: '二次元美食节',
    description: '探索二次元世界中的美食文化，参与互动赢取精美周边！',
    image: '/src/assets/二次元美食节海报_3.jpg',
    startTime: '2024-05-01T00:00:00Z',
    endTime: '2024-05-15T23:59:59Z',
    status: 'ongoing',
    participants: 15420,
    createdAt: new Date().toISOString()
  },
  {
    id: 'activity2',
    title: '前端技术沙龙',
    description: '汇聚前端领域专家，分享最新技术动态和实践经验。',
    image: '/src/assets/科技数码展宣传图.jpg',
    startTime: '2024-06-10T14:00:00Z',
    endTime: '2024-06-10T18:00:00Z',
    status: 'upcoming',
    participants: 892,
    createdAt: new Date().toISOString()
  },
  {
    id: 'activity3',
    title: '动漫嘉年华',
    description: '动漫作品展示、cosplay比赛、周边售卖，一场二次元爱好者的盛会。',
    image: '/src/assets/鬼灭之刃.jpeg',
    startTime: '2024-04-20T09:00:00Z',
    endTime: '2024-04-22T18:00:00Z',
    status: 'ongoing',
    participants: 32567,
    createdAt: new Date().toISOString()
  }
]

// 初始动漫数据
const initialAnimes = [
  {
    id: 'anime1',
    title: '鬼灭之刃 无限列车篇',
    season: '2024春季',
    genre: ['热血', '奇幻', '冒险'],
    thumbnail: '/src/assets/鬼灭之刃.jpeg',
    coverImage: '/src/assets/鬼灭之刃.jpeg',
    synopsis: '大正时期，日本。心地善良的卖炭少年·炭治郎，有一天他的家人被鬼杀死了，而唯一幸存下来的妹妹祢豆子也变成了鬼。为了寻求让妹妹变回人类的方法，炭治郎踏上了「鬼杀队」的道路。',
    episodes: 12,
    currentEpisode: 5,
    rating: 9.5,
    isNew: true,
    updateDay: '周日',
    viewers: 1250000,
    createdAt: new Date().toISOString()
  },
  {
    id: 'anime2',
    title: '黑神话：悟空',
    season: '2024春季',
    genre: ['动作', '玄幻', '冒险'],
    thumbnail: '/src/assets/黑神话.jpeg',
    coverImage: '/src/assets/黑神话.jpeg',
    synopsis: '五百年前，孙悟空大闹天宫，被如来佛祖压在五行山下。五百年后，一位年轻的和尚踏上了西天取经之路，与孙悟空结下不解之缘。',
    episodes: 24,
    currentEpisode: 3,
    rating: 9.8,
    isNew: true,
    updateDay: '周五',
    viewers: 2350000,
    createdAt: new Date().toISOString()
  },
  {
    id: 'anime3',
    title: '进击的巨人 最终季',
    season: '2024春季',
    genre: ['热血', '奇幻', '悬疑'],
    thumbnail: '/src/assets/悟空.jpg',
    coverImage: '/src/assets/悟空.jpg',
    synopsis: '故事发生在一个被巨大围墙环绕的世界，人类被迫躲在墙内生活，墙外游荡着身形巨大的神秘生物「巨人」。主人公艾伦·耶格尔发誓要将巨人全部驱逐出人类的领地。',
    episodes: 16,
    currentEpisode: 8,
    rating: 9.7,
    isNew: false,
    updateDay: '周一',
    viewers: 1850000,
    createdAt: new Date().toISOString()
  }
]

// 初始化数据库
export async function initializeDatabase(options = {}) {
  try {
    console.log('开始初始化数据库...')
    
    // 如果强制重置，先清空所有数据
    if (options.forceReset) {
      console.log('强制重置数据库...')
      await resetDatabase()
    }
    
    // 检查是否已有数据
    const existingUsers = await dataService.users.getAll()
    if (existingUsers.length > 0) {
      console.log('数据库中已存在数据，跳过初始化')
      return { success: true, message: '数据库中已存在数据' }
    }
    
    // 导入初始数据
    console.log('导入初始分类数据...')
    for (const category of initialCategories) {
      await dbService.addDocument(COLLECTIONS.CATEGORIES, category)
    }
    
    console.log('导入初始用户数据...')
    for (const user of initialUsers) {
      await dbService.addDocument(COLLECTIONS.USERS, user)
    }
    
    console.log('导入初始标签数据...')
    for (const tag of initialTags) {
      await dbService.addDocument(COLLECTIONS.TAGS, { 
        id: dbService.generateId(), 
        ...tag,
        createdAt: new Date().toISOString() 
      })
    }
    
    console.log('导入初始视频数据...')
    for (const video of initialVideos) {
      await dbService.addDocument(COLLECTIONS.VIDEOS, video)
    }
    
    console.log('导入初始话题数据...')
    for (const topic of initialTopics) {
      await dbService.addDocument(COLLECTIONS.TOPICS, topic)
    }
    
    console.log('导入初始活动数据...')
    for (const activity of initialActivities) {
      await dbService.addDocument(COLLECTIONS.ACTIVITIES, activity)
    }
    
    console.log('导入初始动漫数据...')
    for (const anime of initialAnimes) {
      await dbService.addDocument(COLLECTIONS.ANIMES, anime)
    }
    
    console.log('数据库初始化完成！')
    return { 
      success: true, 
      message: '数据库初始化成功',
      importedCollections: Object.values(COLLECTIONS)
    }
  } catch (error) {
    console.error('数据库初始化失败:', error)
    return { success: false, error: error.message }
  }
}

// 重置数据库
export async function resetDatabase() {
  try {
    console.log('重置数据库...')
    // 清空所有集合
    for (const collection of Object.values(COLLECTIONS)) {
      await dbService.clearCollection(collection)
    }
    // 清除localStorage中的数据库版本信息
    localStorage.removeItem('db_version')
    console.log('数据库重置完成')
    return { success: true }
  } catch (error) {
    console.error('重置数据库失败:', error)
    return { success: false, error: error.message }
  }
}

// 导出所有初始数据，供测试使用
export const allInitialData = {
  users: initialUsers,
  videos: initialVideos,
  tags: initialTags,
  topics: initialTopics,
  activities: initialActivities,
  animes: initialAnimes,
  categories: initialCategories
}

export default initializeDatabase